package xyz.chenyongpeng.orderproviderdubbo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import xyz.chenyongpeng.entity.ZsCoupon;
import xyz.chenyongpeng.orderproviderdubbo.mapper.ZsCouponMapper;
import xyz.chenyongpeng.service.IZsCouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.chenyongpeng.to.CouponTo;
import xyz.chenyongpeng.to.MyCoupons;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 优惠券 服务实现类
 * </p>
 *
 * @author chenyongpeng
 * @since 2018-12-10
 */
public class ZsCouponService extends ServiceImpl<ZsCouponMapper, ZsCoupon> implements IZsCouponService {

    @Autowired
    private ZsCouponMapper zsCouponMapper;

    @Override
    public void getCouponIdByGoodsId(String[] goods_id) {

    }

    @Override
    public List<Long> getGoodsIdByCouponId(String coupon_id) {
        return zsCouponMapper.getGoodsIdByCouponId(coupon_id);
    }

    @Override
    public Integer getCouponLost(String u_id) {
        int start = (int) (System.currentTimeMillis() / 1000);
        int end = (int) (System.currentTimeMillis() / 1000) + 7 * 24 * 3600;
        Map<String,Object> parms = new HashMap<>();
        parms.put("u_id", u_id);
        parms.put("start", start);
        parms.put("end", end);
        return zsCouponMapper.getCouponLost(parms);
    }

    @Override
    public List<CouponTo> getListByLimit(Map<String, Object> parms) {
        String[] pages = parms.get("limit").toString().split(",");
        parms.put("start", Integer.parseInt(pages[0]));
        parms.put("end", Integer.parseInt(pages[1]));
        return zsCouponMapper.getListByLimit(parms);
    }

    @Override
    public int countByCoupon(Map<String, Object> parms) {
        return zsCouponMapper.countByCoupon(parms);
    }

    @Override
    public List<MyCoupons> getMyCoupons(Map<String, Object> parms) {
        if (!StringUtils.isEmpty(parms.get("limit"))){
            String[] pages = parms.get("limit").toString().split(",");
            parms.put("start", Integer.parseInt(pages[0]));
            parms.put("end", Integer.parseInt(pages[1]));
        }
        return zsCouponMapper.getMyCoupons(parms);
    }

    @Override
    public int getMyCouponsCount(Map<String, Object> parms) {
        return zsCouponMapper.getMyCouponsCount(parms);
    }

    /**
     * 获取一个商品对应的优惠券信息
     * @param parms
     */
    @Override
    public List<MyCoupons> getCouponListByGoodsId(Map<String, Object> parms) {
        return zsCouponMapper.getCouponListByGoodsId(parms);
    }
}
